package controller

import (
	"github.com/gin-gonic/gin"
	logging "github.com/sirupsen/logrus"
	"path"
	"webdemo/src/gin/base"
)

func FileUpload(ctx *gin.Context) {
	// 读取文件
	// 多个文件
	/*form, err2 := ctx.MultipartForm()
	files := form.File["file"]*/
	file, err := ctx.FormFile("file")
	if err != nil {
		ctx.JSON(500, &base.Response{
			Success: false,
			Code:    500,
			Msg:     err.Error(),
		})
		return
	}
	// 保存文件在本地
	//dst := fmt.Sprintf("./%s", file.Filename)
	dst := path.Join("./", file.Filename)
	logging.Info("path.Join dst is ", dst)
	err = ctx.SaveUploadedFile(file, dst)
	if err != nil {
		ctx.JSON(500, &base.Response{
			Success: false,
			Code:    500,
			Msg:     err.Error(),
		})
		return
	}
	ctx.JSON(200, &base.Response{
		Success: true,
		Code:    200,
	})
}
